<?
$sub_menu = "230520";
include_once('_common.php');

auth_check($auth[$sub_menu], "r");

if (empty($fr_date)) // 시작날짜 초기화
    $fr_date = date("Y-m-01", $g4[server_time]);

if (empty($to_date)) // 종료날짜 초기화
    $to_date = date("Y-m-t", $g4[server_time]);

$qstr = "fr_date=$fr_date&to_date=$to_date";

$g4[title] = '컨텐츠별 통계';

$colspan = 5;

include_once("$g4[admin_path]/admin.head.php");
?>

<?=subtitle($g4[title])?>

<script type="text/javascript">
function fcount_submit(ymd, sdate, edate) 
{
    var f = document.fcount;
    f.ymd.value = ymd;
    if (sdate) f.fr_date.value = sdate;
    if (edate) f.to_date.value = edate;
    f.action = "<?=$PHP_SELF?>";
    f.submit();
}
</script>

<form name=fcount method=get style="margin:0;">
<input type=hidden name="ymd">
<table width=100% cellpadding=3 cellspacing=1>
<tr>
    <td>
        기간 : 
        <input type='text' name='fr_date' size=11 maxlength=10 value='<?=$fr_date?>' class=ed> -
        <input type='text' name='to_date' size=11 maxlength=10 value='<?=$to_date?>' class=ed>
        &nbsp;
        <input type=button class=btn1 value=' 검색 ' onclick="fcount_submit('d');">
        <input type=button class=btn1 value=' 이번달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", $g4[server_time])?>', '<?=date("Y-m-t", $g4[server_time])?>');">
        <input type=button class=btn1 value=' 지난달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", strtotime("-1 month", strtotime("$fr_date 00:00:00")))?>',
                '<?=date("Y-m-t", strtotime("-1 month", strtotime("$fr_date 00:00:00")))?>');">
        <input type=button class=btn1 value=' 다음달 '
            onclick="fcount_submit('d', '<?=date("Y-m-01", strtotime("+1 month", strtotime("$fr_date 00:00:00")))?>',
                '<?=date("Y-m-t", strtotime("+1 month", strtotime("$fr_date 00:00:00")))?>');">
        <input type=button class=btn1 value=' 상반기 '
            onclick="fcount_submit('m', '<?=date("Y-01-01")?>', '<?=date("Y-06-30")?>');">
        <input type=button class=btn1 value=' 하반기 '
            onclick="fcount_submit('m', '<?=date("Y-07-01")?>', '<?=date("Y-12-31")?>');">
        <input type=button class=btn1 value=' 올해 '
            onclick="fcount_submit('m', '<?=date("Y-01-01")?>', '<?=date("Y-12-31")?>');">

        <input type=button class=btn1 value=' 전체 ' onclick="fcount_submit('d', '2000-01-01', '<?=date("Y-m-t", $g4[server_time])?>')">
        &nbsp;&nbsp;
    </td>
</tr>
</table>
</form>


<table width=100% cellpadding=0 cellspacing=1 border=0>
<colgroup width=''>
<colgroup width=80>
<colgroup width=80>
<colgroup width=80>
<colgroup width=300>
<tr><td colspan='<?=$colspan?>' class='line1'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td>컨텐츠명</td>
    <td><?=subject_sort_link('cnt', $qstr)?>구매수</a></td>
    <td><?=subject_sort_link('cash', $qstr)?><?=$mw_cash[cf_cash_name]?></a></td>
    <td>비율(%)</td>
    <td>그래프</td>
</tr>
<tr><td colspan='<?=$colspan?>' class='line2'></td></tr>
<?
$list = array();
$max = $sum = $sum_cash = 0;

$sql_select = " rel_table, rel_id, sum(cl_cash) as cash, count(*) as cnt ";
$sql_common = " from $mw_cash[cash_list_table] ";
$sql_search = " where cl_datetime between '$fr_date 00:00:00' and '$to_date 23:59:59' ";
$sql_search .= " and cl_cash < 0 and cl_status = '1' ";
$sql_group  = " group by rel_table, rel_id";
$sql_order = " order by cl_datetime desc ";

switch ($ymd) {
    case "y":
        $sql_select .= ", left(cl_datetime, 4) as day ";
        break;
    case "m":
        $sql_select .= ", left(cl_datetime, 7) as day  ";
        break;
    default:
	$ymd = "d";
        $sql_select .= ", left(cl_datetime, 10) as day  ";
        break;
}

$sql = "select
	$sql_select
        $sql_common 
        $sql_search 
        $sql_group 
        $sql_order";
$qry = sql_query($sql);

for ($i=0; $row=sql_fetch_array($qry); $i++)
{
    $list[$i] = $row;

    if ($row[cnt] > $max)
	$max = $row[cnt];

    $sum += $row[cnt];
    $sum_cash += $row[cash];

    if ($row[rel_table] == "membership" && $row[rel_id]) {
        $sql2 = "select * from $mw_cash[membership_table] where ms_id = '$row[rel_id]'";
        $row2 = sql_fetch($sql2, false);

        $list[$i][wr_subject] = $row2[ms_name];
    }
    elseif ($row[rel_table] == "pay_banner" && $row[rel_id]) {

        $sql2 = "select * from $pb[config_table]";
        $row2 = sql_fetch($sql2, false);

        $sql3 = "select * from $pb[product_table] where pr_id = '$row[rel_id]'";
        $row3 = sql_fetch($sql3, false);

        $list[$i][wr_subject] = "$row2[cf_name] $row3[pr_name]";
    }
    else if ($row[rel_table] && $row[rel_id]) {
        $sql2 = "select wr_subject from $g4[write_prefix]$row[rel_table] where wr_id = '$row[rel_id]'";
        $row2 = sql_fetch($sql2, false);

        $list[$i][wr_subject] = $row2[wr_subject];
    }
    else if ($row[rel_table] && !$row[rel_id]) {
        $sql2 = "select * from $g4[board_table] where bo_table = '$row[rel_table]'";
        $row2 = sql_fetch($sql2, false);

        $list[$i][wr_subject] = $row2[bo_subject];
    }

    if (!trim($list[$i][wr_subject])) {
        $list[$i][wr_subject] = "기타";
    }
}
$total_count = count($list);

/*
$rows = $config[cf_page_rows];
$total_page  = ceil($total_count / $rows);  // 전체 페이지 계산
if (!$page) $page = 1; // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함
*/

if ($sst && $sod) {

    $tmp = array();
    foreach ($list as $key => $val) {
        $tmp[$key] = $val[$sst];
    }

    if ($sod == "desc")
        $sort = SORT_DESC;
    else
        $sort = SORT_ASC;

    array_multisort($tmp, $sort, $list);
}

//for ($i=$from_record; $i<$from_record+$rows; $i++) { 
for ($i=0; $i<$total_count; $i++) { 
    if ($i>=$total_count) break;

    $per1 = @round($list[$i][cnt] / $sum * 100);
    $per2 = @round($list[$i][cnt] / $max * 100);
    $day = $list[$i][day];

    $subject = "<a href='mw.cash.list.php?rel_table={$list[$i][rel_table]}&rel_id={$list[$i][rel_id]}";
    $subject.= "&to_date=$to_date&fr_date=$fr_date";
    $subject.= "'>".cut_str($list[$i][wr_subject], 50)."</a>";

    $l = ($i%2);
    echo "
    <tr class='list$l col1 ht center'>
	<td align=left> &nbsp;{$subject} </td>
	<td> ".number_format($list[$i][cnt])." </td>
	<td> ".number_format($list[$i][cash])." </td>
	<td> $per1% </td>
	<td align=left> 
	    <img src='$g4[admin_path]/img/graph.gif' width='$per2%' height='18'>
	</td>
    <tr>";
}

if ($i == 0)
    echo "<tr><td colspan='$colspan' height=100 align=center>자료가 없습니다.</td></tr>"; 
else {
    echo "<tr><td colspan='$colspan' class='line2'></td></tr>
<tr class='bgcol1 bold col1 ht center'>
    <td>합계</td>
    <td>".number_format($sum)."</td>
    <td>".number_format($sum_cash)."</td>
    <td>100%</td>
    <td>&nbsp;</td>
</tr>";
}

echo "<tr><td colspan='$colspan' class='line1'></td></tr>";
echo "</table>";

$page = get_paging($config[cf_write_pages], $page, $total_page, "$_SERVER[PHP_SELF]?$qstr&domain=$domain&page=");
if ($page) {
    echo "<table width=100% cellpadding=3 cellspacing=1><tr><td align=right>$page</td></tr></table>";
}

include_once("$g4[admin_path]/admin.tail.php");
?>
